﻿using BaseUtils.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using 拧紧抢反转程序.froms;

namespace UserManage
{
    public partial class UserIndex : Form
    {
        public static UserConfig AllUser;
        public static BindingList<UserInfo> TableUser;

        public delegate void UpdateUserMsg(UserInfo old, UserInfo _new);
        public delegate void AddUserMsg(UserInfo _new);
        public delegate void DeleteUserMsg(UserInfo delete);

        public event UpdateUserMsg OnUpdateUser;
        public event AddUserMsg OnAddUser;
        public event DeleteUserMsg OnDeleteUserMsg;

        public UserIndex()
        {
            InitializeComponent();
            LoadingUserList();

            UpdateTable();

        }

        /// <summary>
        /// 加载用户列表
        /// </summary>
        private void LoadingUserList()
        {
            AllUser = Authentication.Users;
            foreach (UserInfo item in AllUser.List1)
            {
                if (string.IsNullOrEmpty(item.UserName))
                {
                    item.UserName = item.User.Substring(0, 3) + "*****";
                }
            }
        }

        /// <summary>
        /// 新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            new AddUser(this).Show();
        }

        internal void AddUser(UserInfo userInfo)
        {
            userInfo.UserName = "";
            userInfo.Visits = new List<Visit>();
            new AuthorityManage(this, userInfo).Show();
        }

        internal void DeleteUser(UserInfo userInfo)
        {
            userInfo.UserName = userInfo.User.Substring(0, 3) + "*****";
            new AuthorityManage(this, userInfo).Show();
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                return;
            }
            if (!Authentication.IsAuthenticated())
            {
                MessageBox.Show("请退出重试");
                return;
            }
            int Index = dataGridView1.CurrentRow.Index;
            UserInfo deleteUser = TableUser[Index];
            if (deleteUser.User == Authentication.CurrentLoginUser.User)
            {
                MessageBox.Show("无法自己删除自己");
                return;
            }
            if (MessageBox.Show($"是否删除{deleteUser.UserName}", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                AllUser.List1.Remove(deleteUser);
                TableUser.Remove(deleteUser);
                XmlHelper.Serializer(AllUser, Authentication.UserConfigFile);
                OnDeleteUserMsg(deleteUser);
            }
        }

        /// <summary>
        /// 更新或新增用户
        /// </summary>
        /// <param name="userInfo"></param>
        internal void UpdateUser(UserInfo userInfo)
        {
            int ex = AllUser.List1.Select((ui, ind) => new { Key = ui.User, index = ind }).FirstOrDefault(x => x.Key == userInfo.User)?.index ?? -1;

            if (ex >= 0)
            {
                UserInfo old = AllUser.List1[ex];
                AllUser.List1.RemoveAt(ex);
                AllUser.List1.Insert(ex, userInfo);
                OnUpdateUser?.Invoke(old, userInfo);
            }
            else
            {
                AllUser.List1.Add(userInfo);
                OnAddUser(userInfo);
            }
            UpdateTable();
            XmlHelper.Serializer(AllUser, Authentication.UserConfigFile);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                return;
            }
            int i = dataGridView1.CurrentRow.Index;
            UserInfo u = TableUser[i];

            new AuthorityManage(this, u).Show();
        }

        private void UpdateTable()
        {
            if (Authentication.CurrentLoginUser?.Level == 5)
            {
                TableUser = AllUser.List1;
            }
            else
            {
                TableUser = new BindingList<UserInfo>(AllUser.List1.Where(au => Authentication.CurrentLoginUser?.Level >= au.Level).ToList());
            }

            dataGridView1.DataSource = TableUser;
        }
    }
}
